package pl.textureStudio.view.panel.create
{
	import com.bit101.components.InputText;
	import com.bit101.components.Label;
	import flash.events.MouseEvent;
	import pl.textureStudio.data.vo.StudioTextureVO;
	import pl.textureStudio.view.panel.base.BasePanel;
	import pl.textureStudio.view.panel.edit.TextureView;

	import com.bit101.components.PushButton;

	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.net.FileFilter;

	/**
	 * @author michalr
	 */
	public class CreateTexturePanel extends BasePanel
	{
		private var panel:Shape;
		private var frame:Shape;
		private var textureView:TextureView;
		private var bNext:PushButton;
		private var bClose:PushButton;
		// ...
		private static const FRAME_SIZE:uint = 10;
		internal static const PANEL_W:uint = 150;

		public function CreateTexturePanel()
		{
			super();
		}

		override protected function init():void
		{
			panel = new Shape();
			frame = new Shape();
			var g:Graphics;
			// ...
			g = panel.graphics;
			g.beginFill( 0x333333 );
			g.drawRect( FRAME_SIZE, FRAME_SIZE, PANEL_W, size.y - FRAME_SIZE * 2 );
			g.endFill();
			// ...
			g = frame.graphics;
			g.beginFill( 0, 1 );
			g.drawRect( 0, 0, size.x, size.y );
			g.drawRect( FRAME_SIZE, FRAME_SIZE, size.x - FRAME_SIZE * 2, size.y - FRAME_SIZE * 2 );
			g.endFill();
			g.beginFill( 0x3A3A3A, .9 );
			g.drawRect( FRAME_SIZE, FRAME_SIZE, size.x - FRAME_SIZE * 2, size.y - FRAME_SIZE * 2 );
			g.endFill();
			addChild( frame );
			addChild( panel );

			// ...
			// ui buttons
			
			var leftMargin:uint = 15;
			var sectionCloseSave:UISection = new UISection( this, FRAME_SIZE, 392, PANEL_W, 'Options' );
			bClose = new PushButton( this, leftMargin, 0, 'Close ', onClosePanel );
			bClose.setSize( 40, bClose.height );
			bNext = new PushButton( this, leftMargin + 75, 0, 'Next', onNext);
			bNext.setSize( 40, bNext.height );
			sectionCloseSave.setComponent( bClose, 0 );
			sectionCloseSave.setComponent( bNext, 0 );
			
			// ...
			// ui inputs
			
			var sectionInputs : UISection = new UISection(this, FRAME_SIZE, 10,PANEL_W, 'Config');
			var nameLabel : Label = new Label(this,leftMargin,10,'Set Name');
			var nameInput : InputText = new InputText(this,leftMargin,10,'');
			nameLabel.textField.textColor = 0xFFFFF0; 
			sectionInputs.setComponent(nameLabel, 0);
			sectionInputs.setComponent(nameInput, 20);
			// ...
			var tempFiles:File = File.applicationStorageDirectory;
			tempFiles = tempFiles.resolvePath( "images/" );
			var file:File = File.applicationDirectory;
			file.nativePath += '/textureStudio/textures/';
			file.addEventListener( Event.SELECT, onFileBrowseSelect );
			var fileFilter:FileFilter = new FileFilter( 'Obrazki', '*.png;*.jpg' );
			file.browseForOpen( 'Wybierz texture do za�adowania', [fileFilter] );
			
		}

		private function onClosePanel(e:MouseEvent):void
		{
			
		}

		private function onNext(e:MouseEvent):void
		{
			
		}


		private function onFileBrowseSelect( event:Event ):void
		{
			var file:File = event.target as File;
			textureView = TextureView.factory( StudioTextureVO.factory2( file.nativePath ), onFileReady );
			addChild( textureView );
		}

		private function onFileReady():void
		{
			textureView.x = PANEL_W + (size.x - textureView.getSize().x -  FRAME_SIZE - PANEL_W) * .5;
			textureView.y = (size.y - textureView.getSize().y - 2 * FRAME_SIZE) * .5;
		}
	}
}
import com.bit101.components.Component;
import com.bit101.components.Label;

import flash.display.DisplayObjectContainer;
import flash.display.Shape;
import flash.display.Sprite;

class UISection extends Sprite
{
	private var sectionLine:Shape;
	private var topMargin:uint;
	private var w:uint;
	private var label:Label;

	public function UISection( parent:DisplayObjectContainer, px:uint, py:uint, w:uint, sectionName:String )
	{
		y = py;
		x = px;
		parent.addChild( this );
		sectionLine = new Shape();
		sectionLine.graphics.lineStyle( 1, 0x1E1E1E, 1, false, 'none' );
		sectionLine.graphics.lineTo( w, 0 );
		sectionLine.graphics.moveTo( 0, 1 );
		sectionLine.graphics.lineStyle( 1, 0x4D4D4D, 1, false, 'none' );
		sectionLine.graphics.lineTo( w, 1 );
		label = new Label( this, 0, 1, sectionName );
		sectionLine.graphics.moveTo( 0, Math.floor( label.y + label.height ) );
		sectionLine.graphics.lineStyle( 1, 0x4D4D4D, 1, false, 'none' );
		sectionLine.graphics.lineTo( w, Math.floor( label.y + label.height ) );
		addChild( sectionLine );
		topMargin = 5;
		this.w = w;
	}

	public function setComponent( component:Component, py:Number ):void
	{
		addChild( component );
		component.y = py + topMargin + label.y + label.height;
	}
}
